package KK.WindowSkin
{
	import KK.Controls.Button.BaseButton;
	import KK.Controls.Button.TextButton;
	import KK.Data.Scale.Scale16;
	import KK.EMVC.View.View;
	import KK.Hunter.Window.IWindow;
	import KK.Manager.IManager;
	import KK.System.System; 
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class Window extends View implements IWindow
	{
		private static const MAX_GRID:int=9;
		static private const T_HEIGHT:int=42;//标题高度 
		
		private var Area:Vector.<Rectangle>;
		private var sheet:SkinSheet;//皮肤区域数据 
		private var _title:TitleSheet;//标题
		
		private var _BClose:BaseButton;//关闭按钮
		
		private var sourceSkin:WindowSkin;//皮肤源数据
		private var skin:Bitmap;//皮肤
		
		private var content:Sprite;//窗口内容 
		
		
		private var drawState:Boolean;
		private var _handle:int;///句柄id
		public function Window()
		{
			//TODO: implement function
			super();
			sheet=new SkinSheet();
			_title=new TitleSheet();
			content=new Sprite;
			_BClose=new BaseButton();
			
			setSize(20,20);
			init();
		}
		
		private function init(event:Event=null):void{
			setSheet(sheet);
			
			
			GetMC().addEventListener(MouseEvent.MOUSE_DOWN,MouseDown);
			GetMC().addEventListener(MouseEvent.MOUSE_UP,MouseUp);
			addChild(content);
			
			content.x=16;
			content.y=T_HEIGHT;
			
			addChild(_BClose);
			resetLayer();
			
		}
		
		/**
		 * 获取内容宽度 
		 * @return 
		 * 
		 */
		protected function get ContentWidth():Number{
			return content.width;
		}
		
		
		/**
		 * 获取内容高度  
		 * @return 
		 * 
		 */
		protected function get ContentHeight():Number{
			return content.height;
		} 
		
		
		/**
		 * 设置大小  
		 * 
		 */
		public function setSize(width:int,height:int):void{
			drawState=true;
			skin&&mc.removeChild(skin);
			skin=new Bitmap(new BitmapData(width,height+T_HEIGHT,true,0));
			addChild(skin); 
			 
			
		}
		
		
		/**
		 * 设置皮肤 
		 * @param value
		 * 
		 */
		public function set Skin(value:WindowSkin):void{
			sourceSkin=value;
			initSkin();
			resize();
			draw();
		}
		/**
		 * 设置皮肤  
		 * @return 
		 * 
		 */
		public function get Skin():WindowSkin{
			return sourceSkin;
		}
		
		
		
		public function get width():Number{
			return skin.width;
		}
		
		public function get height():Number{
			return skin.height;
		}
		
		/**
		 * 重绘 
		 * 
		 */
		override protected function draw():void{
			sourceSkin&&isRedraw()&&drawSkin();
			resetLayer();
		}
		
		/**
		 * 重置图层  
		 * 
		 */
		protected function resetLayer():void{
			mc.setChildIndex(skin,0);
			mc.setChildIndex(content,1);
		}
		
		
		/**
		 * 添加内容 
		 * @param child
		 * 
		 */
		public function addContent(child:DisplayObject):void{
			content.addChild(child);
			
			resize();
		}
		
		/**
		 * 移除内容  
		 * @param child
		 * 
		 */
		protected function removeContent(child:DisplayObject):void{
			if(child.parent){
				content.removeChild(child);
			}
		}
		
		/**
		 * 绘制皮肤 
		 * 
		 */
		protected function drawSkin():void{
			//splitFillVector();
			drawState=false;
			drawBody();
			drawTitle();
		}
		
		
		/**
		 * 开始标题拖动 
		 * 
		 */
		protected function startTitleDrag():void{ 
			
			if(mc.mouseY<42){
				Sprite(mc).startDrag();
			}
			
		}
		
		/**
		 * 停止拖动 
		 * 
		 */
		protected function stopTitleDrag():void{
			Sprite(GetMC()).stopDrag();
		}
		
		
		/**
		 * 鼠标按下  
		 * @param event
		 * 
		 */
		private function MouseDown(event:MouseEvent):void{
			startTitleDrag();
		}
		
		/**
		 * 鼠标弹起  
		 * @param event
		 * 
		 */
		private function MouseUp(event:MouseEvent):void{
			stopTitleDrag();
		}
		
		
		/**
		 * 绘制窗体 
		 * 
		 */
		protected function drawBody():void{
			
			var list:Vector.<Vector.<Rectangle>>=ConvryScale9(new Point(skin.bitmapData.width,skin.bitmapData.height),Area,new Point());
			
			FillVectorBitmapData(sourceSkin.Body,skin.bitmapData,list[0],list[1]); 
		}
		/**
		 * 绘制标题  
		 * 
		 */
		protected function drawTitle():void{
			var list:Vector.<Vector.<Rectangle>>=_title.calcTarget(width,height);
			FillVectorBitmapData(sourceSkin.Title,skin.bitmapData,list[0],list[1],new Point(10,10)); 
		}
		
		/**
		 * 初始化皮肤  
		 * 
		 */
		private function initSkin():void{
			if(sourceSkin.Close)
			{
				_BClose.Skin=sourceSkin.Close;
				_BClose.addEventListener(MouseEvent.CLICK,onClose);
			}
			
			sourceSkin.Title&&_title.calc(sourceSkin.Title.width,sourceSkin.Title.height);
		}
		
		/**
		 * 关闭  
		 * @param event
		 * 
		 */
		private function onClose(event:MouseEvent):void{
			Hide();
		}
		/**
		 * 设置皮肤数据  
		 * @param sheet
		 * 
		 */
		private function setSheet(sheet:SkinSheet):void{
			
			Area=sheet.Sudoku;
		}
		
		/**
		 * 重置位置  
		 * 
		 */
		private function resize():void{
			
			setSize(ContentWidth+2*content.x,ContentHeight+content.y/2);
			
			_BClose.x=ContentWidth - content.x;
			_BClose.y=12;
		}
		
		
		
		/**
		 * 切割成填充区域数据 
		 * 
		 */
		private function ConvryScale9(targetSize:Point,Area:Vector.<Rectangle>,desPoint:Point):Vector.<Vector.<Rectangle>>{
			var result:Vector.<Vector.<Rectangle>>=new Vector.<Vector.<Rectangle>>(2,true);
			var targetRect:Rectangle=new Rectangle; 
			var i:int;
			
			var targetList:Vector.<Rectangle>=new Vector.<Rectangle>();
			var sourceList:Vector.<Rectangle>=new Vector.<Rectangle>();
			
			targetRect.y=desPoint.y;
			for(i=0;i<MAX_GRID;i++){
				
				if(i%3){
					targetRect.x+=targetRect.width;
				}else{
					targetRect.x=desPoint.x;
					targetRect.y+=targetRect.height;
				}
				
				switch(true){
					case i/4==1:
						//中间块
						targetRect.width=targetSize.x-Area[i-3].width-Area[i+3].width;
						targetRect.height=targetSize.y-Area[i-3].height-Area[i+3].height; 
						break;
					case Math.floor(i/3)==1:
						//col=1
						targetRect.width=Area[i].width;
						targetRect.height=targetSize.y-Area[i-3].height-Area[i+3].height;
						break;
					case i%2==1:
						//横向填充
						targetRect.height=Area[i].height;
						targetRect.width=targetSize.x-Area[i-1].width-Area[i+1].width;
						break;
					default:
						targetRect.width=Area[i].width;
						targetRect.height=Area[i].height;
						
						break;
				}
				sourceList.push(Area[i]);
				targetList.push(targetRect.clone()); 
				
			}
			
			result[0]=sourceList;
			result[1]=targetList;
			return result;
			
//			FillVectorBitmapData(sourceSkin,skin.bitmapData,sourceList,targetList);
		}
		

		
		/**
		 * 填充数组数据 
		 * @param sourceBitmapData
		 * @param target
		 * @param sourceArea
		 * @param targetArea
		 * 
		 */
		private function FillVectorBitmapData(sourceBitmapData:BitmapData,target:BitmapData,sourceArea:Vector.<Rectangle>,targetArea:Vector.<Rectangle>,despoint:Point=null):void{
			var i:int=0;
			const len:int=sourceArea.length;
			if(len!=targetArea.length){
				LogError("绘图数量不对");
			}
			while(i<len){
				fillBitmapData(sourceBitmapData,target,sourceArea[i],targetArea[i],despoint); 
				i++;
			}
		}
		
		/**
		 * 位图填充  
		 * @param sourceBitmapData 填充源图片
		 * @param target		填充目标图片
		 * @param sourceArea	填充源区域
		 * @param target		填充目标区域
		 * 
		 */
		private function fillBitmapData(sourceBitmapData:BitmapData,target:BitmapData,sourceArea:Rectangle,targetArea:Rectangle,despoint:Point=null):void{
			var expoint:Point=new Point(targetArea.x,targetArea.y);
			if(despoint){
				expoint=expoint.add(despoint);
			}
			var targetSize:Point=new Point(targetArea.width+targetArea.x,targetArea.height+targetArea.y); 
			var bool:Boolean=false;
			for(;;){
				switch(true){ 
					case expoint.x<targetSize.x:
						//填充宽度    
						expoint.x=expoint.x+sourceArea.width>targetSize.x?targetSize.x-sourceArea.width:expoint.x;
						if(despoint){
							expoint.x=expoint.x+sourceArea.width>targetSize.x?targetSize.x-sourceArea.width+despoint.x:expoint.x;
							expoint.x=expoint.x>0?expoint.x:targetArea.x+despoint.x;
						}else{
							
							expoint.x=expoint.x+sourceArea.width>targetSize.x?targetSize.x-sourceArea.width:expoint.x;
							expoint.x=expoint.x>0?expoint.x:targetArea.x;
						}
						target.copyPixels(sourceBitmapData,sourceArea,expoint);
						expoint.x+=sourceArea.width;
						break;
					case expoint.y<targetSize.y:
						//换行并且调整位置
						if(despoint){
							expoint.x=targetArea.x+despoint.x;
						}else{
							expoint.x=targetArea.x;
						} 
						expoint.y+=sourceArea.height;
						break; 
					
				}
				if(expoint.y>=targetSize.y){
					return ;
				}
			}
		}
		
		
		
		/**
		 * 是否触发重绘  
		 * @return 
		 * 
		 */
		private function isRedraw():Boolean{
			return drawState;
		}
	}
}